package com.gec.hrm.servlet;

import com.gec.hrm.dao.impl.UserDao;
import com.gec.hrm.dao.impl.UserDaoImpl;
import com.gec.hrm.pojo.R;
import com.gec.hrm.pojo.User;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/userlist.action","/addUser.action","/checkName.action","/updUser.action","/delUsers.action","/delUser.action"})
public class UserServlet extends HttpServlet {
    public UserDao userDao=new UserDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String url = request.getRequestURI();
        System.out.println(url);
        String action = url.substring(url.lastIndexOf("/") + 1);
        System.out.println(action);

        if (action.equals("userlist.action")) {
            userList(request,response);
        } else if(action.equals("checkName.action")) {
            cheackName(request,response);
        }else if(action.equals("addUser.action")){
            addUser(request,response);
        }else if(action.equals("updUser.action")){
            updUser(request,response);
        }else if(action.equals("delUsers.action")){
            delUsers(request,response);
        }else if(action.equals("delUser.action")){
            delUser(request,response);
        }
    }

    private void delUser(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));


        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        try {
            PrintWriter out = response.getWriter();
            if (user.getId() != id) {
                int i = userDao.delUser(id);
                if (i > 0) {
                    out.print(0);
                } else {
                    out.print(-1);
                }
            } else {
                out.print(user.getId());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void delUsers(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        var id = ids.split(",");

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        try {
            PrintWriter out = response.getWriter();
            for (var ix : id) {
                if (user.getId() == Integer.parseInt(ix)) {
                    out.print(user.getId());
                    return;
                }
            }
            for (var ix : id) {
                int i = userDao.delUser(Integer.parseInt(ix));
                if (i > 0) {
                    out.print(0);
                } else {
                    out.print(-1);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void updUser(HttpServletRequest request, HttpServletResponse response) {
        String loginname= request.getParameter("loginname");
        String username= request.getParameter("username");
        int status=Integer.parseInt( request.getParameter("status"));
        int id=Integer.parseInt(request.getParameter("id"));

        User user=new User(id,loginname,status,username);
        int i=userDao.updUser(user);

        try {
            PrintWriter out=response.getWriter();
            if(i>0){
                out.print(1);
            }else{
                out.print(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void addUser(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("addUser");
        String loginname= request.getParameter("loginname");
        String username= request.getParameter("username");
        int status=Integer.parseInt( request.getParameter("status"));
        String password=request.getParameter("password");
        User user=new User(loginname,password,status,username);
        int i=userDao.addUser(user);

        try {
            PrintWriter out=response.getWriter();
            if(i>0){
                out.print(1);
            }else{
                out.print(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void cheackName(HttpServletRequest request, HttpServletResponse response) {
        // 取出用户输入的条件
        String loginname = request.getParameter("loginname");
        boolean flag=userDao.checkName(loginname);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            if(flag){
                out.print(0);
            }else {
                out.print(1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void userList(HttpServletRequest request, HttpServletResponse response) {
        //处理乱码问题
        response.setCharacterEncoding("UTF-8");
        //通过响应的response获取打印输出流
        PrintWriter out = null;
        try {
            out = response.getWriter();
            // 取出用户输入的条件
            String loginname = request.getParameter("username");
            String status = request.getParameter("status");
            //调用查询方法
            int page=Integer.parseInt(request.getParameter("page"));
            int limit=Integer.parseInt(request.getParameter("limit"));
            List<User> users = userDao.userList(loginname, status,page,limit);
            int count= userDao.count();
            //lauyi框架返回四个参数
            R r = new R();
            r.put("msg", "查询成功");
            r.put("data", users);
            r.put("count", count);
            r.put("code", 0);
            //将对象返回到页面上，响应数据--respons--打印输出流
            out.print(new Gson().toJson(r));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("dopost");
        doGet(request,response);
    }
}
